package top.wangjiu.java.jvm.attribute;

import top.wangjiu.java.jvm.AttributeInfo;
import top.wangjiu.java.jvm.attribute.info.LineNumberInfo;
import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;

public class LineNumberTableAttr extends AttributeInfo {

    public final static String NAME = "LineNumberTable";

    private final short attributeNameIndex;   //属性名索引
    private final int attributeLength;
    //字段值在常量池中的索引，常量池在该索引处的项给出该属性表示的常量值。（例如，值是long型的，在常量池中便是CONSTANT_Long）
    private final short lineNumberTableLength;
    private final LineNumberInfo[] lineNumberTable;

    public LineNumberTableAttr(short attributeNameIndex, int attributeLength, LineNumberInfo[] lineNumberTable){
        this.attributeNameIndex = attributeNameIndex;
        this.attributeLength = attributeLength;
        this.lineNumberTableLength = (short) lineNumberTable.length;
        this.lineNumberTable = lineNumberTable;
    }

    @Override
    public short attributeNameIndex() {
        return attributeNameIndex;
    }

    @Override
    public int attributeLength() {
        return attributeLength;
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        StringBuilder sb = new StringBuilder("LineNumberTable:");
        for (int i = 0; i < lineNumberTableLength; i++) {
            sb.append(" {line:"+lineNumberTable[i].getLineNumber()+",startPc:"+lineNumberTable[i].getStartPc()+"}");
        }
        return sb.toString();
    }
}
